package com.kaibes.core.base.util;

import java.nio.charset.Charset;
import java.security.SecureRandom;
import java.util.Base64;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;

public class DesUtils {
    public static Charset CHAR_SET = Charset.forName("utf-8");
    public static String CRYPT_KEY = "yixinhealthrick"; 
    private final static String DES = "DES"; 
         
    public static byte[] encryptBase64(byte[] data, String key) {
        if(key==null||key.length()==0) { 
            key = CRYPT_KEY; 
        } 
        Cipher cipher;
        try {
            cipher = getCipher(Cipher.ENCRYPT_MODE, key);
            byte[] byteTemp = cipher.doFinal(data);
            data = Base64.getEncoder().encode(byteTemp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
    public static String encryptBase64(String data, String key) {
        return new String(encryptBase64(data.getBytes(CHAR_SET), key), CHAR_SET);
    }
    
    public static byte[] decryptBase64(byte[] data, String key) {
        if(key==null||key.length()==0) { 
            key = CRYPT_KEY; 
        } 
        byte[] byteTemp = Base64.getDecoder().decode(data);
        Cipher cipher;
        try {
            cipher = getCipher(Cipher.DECRYPT_MODE, key);
            data = cipher.doFinal(byteTemp);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return data;
    }
    public static String decryptBase64(String data, String key) {
        return new String(decryptBase64(data.getBytes(CHAR_SET), key), CHAR_SET);
    }
    
    public static byte[] encryptByte(byte[] data, String key){  
        if(key==null||key.length()==0) { 
            key = CRYPT_KEY; 
        } 
        try { 
            Cipher cipher = getCipher(Cipher.ENCRYPT_MODE,key);   
            data = cipher.doFinal(data); 
        } catch(Exception e) { 
            e.printStackTrace();
        } 
        return data; 
    } 
    public static String encryptByte(String data, String key){  
        return Byte2String(encryptByte(data.getBytes(), key)); 
    } 
    
    public static byte[] decryptByte(byte[] data, String key) {  
        if(key==null||key.length()==0) { 
            key = CRYPT_KEY; 
        } 
        Cipher cipher = null; 
        try { 
            cipher = getCipher(Cipher.DECRYPT_MODE,key); 
            data = cipher.doFinal(data);
        } catch(Exception e) { 
            e.printStackTrace();
        } 
        return data;
    }  
    public static String decryptByte(String data, String key) {  
        return new String(decryptByte(String2Byte(data), key), CHAR_SET);
    }  
    
    public static Cipher getCipher(int mode,String key) throws Exception { 
        SecureRandom sr = new SecureRandom();  
        DESKeySpec dks = new DESKeySpec(key.getBytes());  
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);  
        SecretKey securekey = keyFactory.generateSecret(dks);  
        Cipher cipher = Cipher.getInstance(DES);  
        cipher.init(mode, securekey, sr);  
        return cipher; 
    } 
     
    public static String Byte2String(byte[] bb) { 
        StringBuilder buff = new StringBuilder(bb.length); 
        String sTemp; 
        for(int i=0;i<bb.length;i++){ 
            sTemp = Integer.toHexString(0xFF &bb[i]); 
            if(sTemp.length()<2){ 
                buff.append(0); 
            } 
            buff.append(sTemp.toUpperCase()); 
        } 
        return buff.toString(); 
    } 
     
    public static byte[] String2Byte(String src) { 
        int len = (src.length()/2); 
        byte [] result = new byte[len]; 
        char[] achar = src.toString().toCharArray(); 
        for(int j=0;j<len;j++){ 
            int pos = j*2; 
            result[j]= ((byte)(Char2Byte(achar[pos])<<4|Char2Byte(achar[pos+1]))); 
        } 
        return result; 
    } 
     
    private static byte Char2Byte(char c){ 
        return (byte)"0123456789ABCDEF".indexOf(c); 
    } 
     
}
